package tal.cdm.gateway.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import tal.cdm.gateway.filter.PreLogGatewayFilterFactory;
import tal.cdm.gateway.predicate.BetweenTimeRoutePredicateFactory;

import java.util.Date;

@Configuration
@Slf4j
public class RouteConf {

    @Bean
    public RouteLocator routeLocator1(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("rout3", r -> r.path("/tal/c").uri("localhost:8081/tal/c")).build();
        return routes.build();
    }

    @Bean
    public HostAddrKeyResolver hostAddrKeyResolver() {
        return new HostAddrKeyResolver();
    }

    @Bean
    public PreLogGatewayFilterFactory preLogGatewayFilterFactory() {
        return new PreLogGatewayFilterFactory();
    }

    @Bean
    @Order(2)
    public GlobalFilter logGatewayFilter2() {
        return ((exchange, chain) -> {
            log.info("enter LogGatewayFilter2....." + new Date());
            ServerHttpRequest request = exchange.getRequest().mutate().build();
            ServerWebExchange modifiedRequest = exchange.mutate().request(request).build();
            return chain.filter(modifiedRequest);
        });
    }

    @Bean
    @Order(1)
    public GlobalFilter logGatewayFilter3() {
        return ((exchange, chain) -> {
            log.info("enter LogGatewayFilter3....." + new Date());
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getPath().toString();
            log.info("LogGatewayFilter3 path = " + path);
            if (path.contains("/tal/t2")) {
                return chain.filter(exchange);
            }
            ServerHttpResponse response = exchange.getResponse();
            System.out.println(request.getHeaders());
            if(response.getStatusCode().equals(HttpStatus.OK)){
                //log.info("LogGatewayFilter3 response outer......... ");
            }

            System.out.println("response = " + response);
            System.out.println("request = " + request);
            log.info("request ....  " + request.getHeaders().toString());
            log.info("response ....  " + response.getHeaders().toString());
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                //log.info("LogGatewayFilter3 post......... ");
                System.out.println(response);
                log.info("then response ....  " + response.getHeaders().toString());

                if(response.getStatusCode().equals(HttpStatus.OK)){
                    //log.info("LogGatewayFilter3 response inner......... ");
                }

            }));
        });
    }

    @Bean
    public BetweenTimeRoutePredicateFactory betweenTimeRoutePredicateFactory() {
        return new BetweenTimeRoutePredicateFactory();
    }
}
